// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  if(event.active == 1) {
    return await db.collection('legue').where({
        status: 0
      }).get({
      success: function (res) {
        return res
      }
    });
  } else if(event.active == 0) {
    return await db.collection('legue').where(_.or([{
      legueName: db.RegExp({
        regexp: '.*' + event.value,
        options: 'i',
      })
    },
    {
      introduction: db.RegExp({
        regexp: '.*' + event.value,
        options: 'i',
      })
    },
    {
      demand: db.RegExp({
        regexp: '.*' + event.value,
        options: 'i',
      })
    },
    {
      addressresult: db.RegExp({
        regexp: '.*' + event.value,
        options: 'i',
      })
    },
    {
      leguetagresult: db.RegExp({
        regexp: '.*' + event.value,
        options: 'i',
      })
    },
    {
      username: db.RegExp({
        regexp: '.*' + event.value,
        options: 'i',
      })
    }
  ])).get({
    success: function (res) {
      return res
    }
  });
  } else if(event.active == 2) {
    return await db.collection('legue').where({
      status: 1
      }).get({
      success: function (res) {
        return res
      }
    });
  } else {
    return await db.collection('legue').where({
      status: 2
      }).get({
      success: function (res) {
        return res
      }
    });
  }
  
}